package caro_game;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.net.ssl.SSLSocket;
import javax.swing.JOptionPane;

public class Main_Client extends javax.swing.JFrame {

    static DataOutputStream gui;
    static Socket s;
    static DataInputStream nhan;

    public Main_Client() {
        initComponents();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 371, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 281, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    public void KetNoiSerVer() {
        this.dispose();
        String nhapIP = JOptionPane.showInputDialog("Mời bạn nhập IP hoặc tên máy của máy chủ", "192.168.1.100");
        if (nhapIP!=null) {
            try {
                s = new Socket(nhapIP, 123);
                DataInputStream chuoi_NhapVao = new DataInputStream(s.getInputStream());//là chuỗi đọc dl từ máy chủ nha
                String docdl = chuoi_NhapVao.readLine();
                if (docdl.length() == 5) {
                    String cat = docdl.substring(0, docdl.length() - 1);//cắt kí tự
                    int cong = Integer.parseInt(cat);
                    Server c1 = new Server(cong, nhapIP);
                    c1.banco();
                    c1.setTitle("CHƯƠNG TRÌNH CHƠI CỜ CARO QUA MẠNG LAN");
                    c1.setVisible(true);
                    c1.KetNoiClient();
                    //ĐÓng ket nối
                    s.close();
                } else {
                    String ip = chuoi_NhapVao.readLine();
                    if (docdl.length() == 4) {
                        int cong = Integer.parseInt(docdl);
                        Client c2 = new Client(cong, ip);
                        c2.banco();
                        c2.setTitle("CHƯƠNG TRÌNH CHƠI CỜ CARO QUA MẠNG LAN");
                        c2.setVisible(true);
                        c2.ketnoi();
                        //ĐÓng ket nối
                        s.close();
                    } else {
                    }
                }
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(null, "Không thể kết nối đến máy chủ!\n" + ex);
            }
        } 
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Main_Client.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Main_Client.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Main_Client.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Main_Client.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
//                new Main_Client().setVisible(true);
            }
        });
        Main_Client m = new Main_Client();
        m.setVisible(true);
        m.setTitle("Main client");
        m.KetNoiSerVer();
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
}
